package com.zhentao.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.awt.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * <p>
 * 
 * </p>
 *
 * @author mp
 * @since 2025-03-26
 */
@Getter
@Setter
@TableName("t_user")
public class User implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    @TableId(value = "uid", type = IdType.AUTO)
    private Integer uid;

    private String username;

    @TableField("`password`")
    private String password;

    private String nickname;

    private Integer accountNoExpire;

    private Integer accountNoLock;

    private Integer accountAble;

    private Integer pwdExpire;
    @TableField(exist = false)
    private List<String> perms;
    @TableField(exist = false)
    private List<String> rName;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(perms!=null){
            ArrayList<GrantedAuthority> list = new ArrayList<>();
            for (String perm : perms) {
                list.add(new SimpleGrantedAuthority(perm));
            }
            return list;
        }
        return null;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNoExpire>0;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNoLock>0;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return pwdExpire>0;
    }

    @Override
    public boolean isEnabled() {
        return accountAble>0;
    }
}
